﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PreguntaAlExperto.RulesLayer.Entities;
using PreguntaAlExperto.RulesLayer.Services;

namespace PreguntaAlExperto.Components
{
    public partial class LogIn : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnAuthenticate.Text = Resources.language.LoginLink;
            this.lblUserName.Text = Resources.language.UserName;
            this.lblPassword.Text = Resources.language.Password;
            this.lblLogAsUser.Text = Resources.language.LogAsUser;
            this.lblRecover.Text = Resources.language.RecoverCredentials;
            this.lblEmailRecover.Text = Resources.language.UserEmail;
            this.btnRecover.Value = Resources.language.RecoverButton;
            this.lblRecoverTitle.Text = Resources.language.RecoverCredentialsTitle;
            this.lblNewCredentials.Text = Resources.language.NewCredentialMessage;
            this.btnRecover.Attributes.Add("onclick", "RecoverPassword(document.getElementById('" + this.txtEmailRecover.ClientID + "').value); return false");
            this.txtPassword.Attributes.Add("onkeypress", "return getKey(event)");

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "submitbuttonname", "var submitButton = '" + this.btnAuthenticate.ClientID.ToString() + "';", true);

            if (this.LitteMode)
            {
                this.lblLogAsUser.Visible = false;
                this.lblRecover.Visible = false;
            }

            if (this.ShowErrorMessage)
            {
                this.lblError.Visible = true;
                this.lblError.Text = "Nombre de usuario o contraseña inexistente."; 
            }
        }

        protected void btnAuthenticate_Click(object sender, EventArgs e)
        {
            Publisher _publisher = new Publisher();
            _publisher.Password = this.txtPassword.Text;
            _publisher.Username = this.txtUserName.Text;

            PublisherService _service = new PublisherService();
            try
            {
                _publisher = _service.logUser(_publisher);
                Session.Add("UserID", _publisher.ID_Publisher);
                Session.Add("UserName", _publisher.Name);
                Session.Add("UserLevel", _publisher.Member);
                Session.Add("LoginName", this.txtUserName.Text);
                if (Request.Path.Contains("ingresar.aspx") ||
                    Request.Path.Contains("registrarse.aspx"))
                {
                    Response.Redirect("~/default.aspx");
                }
                else if (Request.Path.Contains("showArticle.aspx"))
                {
                    Response.Redirect(Request.UrlReferrer.ToString());
                }
                else
                {
                    Response.Redirect(Request.Path);
                }

            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                if (Request.Path.Contains("showArticle.aspx"))
                {
                    Response.Redirect(Request.UrlReferrer.ToString() + "?f=1");
                }
            }
        }

        public bool LitteMode
        {
            get;
            set;
        }

        public bool ShowErrorMessage
        {
            get;
            set;
        }
    }
}